<template>
    <div class="container">
        <div class="title ver">课程资料</div>
        <div class="head">
            <ElImage :src="conf.server + theCourseInfo?.cover" class="portrait" fit="cover"></ElImage>
            <div class="name">
                {{ theCourseInfo?.name }}
            </div>
            <!-- <div class="blocks">
                <div class="block"></div>
            </div> -->
        </div>
        <div class="line ver">
            <!-- <div class="intro">
                <ElImage :src="conf.server + currentUser.portrait" class="portrait" fit="cover"></ElImage>
                
            </div> -->

            <fieldset class="field" v-if="!poss">
                <legend class="legend">
                    <span>课程信息</span>
                </legend>
                <div class="item">
                    <div class="attr">课程名称</div>
                    <div class="content">{{ theCourseInfo?.name }}</div>
                </div>
                <div class="item">
                    <div class="attr">课程介绍</div>
                    <div class="content int">{{ theCourseInfo?.introduction || '暂无' }}
                        <!-- <ElButton @click="changeNumber" link style="text-decoration: underline;float:right;" type="primary">修改学号</ElButton> -->
                    </div>
                </div>
                <div class="item">
                    <div class="attr">任课教师</div>
                    <div class="content">
                        {{ teacher?.name }}
                    </div>
                </div>
                <div class="item">
                    <div class="attr">开课时间</div>
                    <div class="content">
                        {{theCourseInfo?.createdTime.format('YYYY-MM-DD')}}
                    </div>
                </div>
                <div class="item">
                    <div class="attr">学校</div>
                    <div class="content">
                        {{theCourseInfo?.school}}
                    </div>
                </div>
            </fieldset>

            <fieldset class="field stu-list" v-else>
                <legend class="legend">
                    <span>学生管理</span>
                    <!-- <span>学生管理</span> -->
                    <!-- <ElButton style="height: 25px;">添加学生</ElButton> -->
                </legend>
                <div class="item" @mouseenter="currentPointer = stu._id" @mouseleave="currentPointer = ''" v-for="stu in stus" :key="stu._id">
                    <div class="attr">{{ stu.name }}</div>
                    <div class="content">{{ stu.number }}
                        <ElButton @click="remove(stu._id)" v-show="currentPointer === stu._id" link style="text-decoration: underline;float:right;" type="primary">移除</ElButton>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>

</template>

<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue';
import { storeToRefs } from 'pinia';
import { useClientStore } from '../pinia/stores/client';
import { conf } from '@/utils/conf';
import { schools } from '../shared/types/schools';
import { courses } from '@/shared/types/courses';
import { useRoute, useRouter } from 'vue-router';
import { users } from '../shared/types/user';
import { findIndex } from 'lodash';

let loadingInstance: any
const { currentUser, client } = storeToRefs(useClientStore())
const route = useRoute()
const router = useRouter()
const courseId = route.params.courseId as string

const theCourseInfo = ref<courses>()
const teacher = ref<users>()
const stus = ref<users[]>()

const poss = computed(() =>{
    return currentUser.value.role === 'teacher'
})

const currentPointer = ref('')

const remove = async (stuId:string) =>{
    const ret = await client.value.callApi('Remove/RemoveStu',{
        courseId,
        stuId
    })
    if(ret.isSucc){
        ElMessage.success('移除成功')
        stus.value?.splice(stus.value.findIndex(s => s._id === stuId),1)
    }
}

onMounted(async () => {
    loadingInstance = ElLoading.service({
        target: '.container'
    })
    if(!courseId){
        loadingInstance.close()
        ElMessage.error('不存在')
        router.back()
        return
    }
    const ret = await client.value.callApi('Get/GetCourseInfo',{
        course:courseId
    })
    if(ret.isSucc){
        theCourseInfo.value = ret.res.courseInfo
        const ret2 = await client.value.callApi('Get/GetTeacher',{
            teacher:theCourseInfo.value.teacher
        })
        if(ret2.isSucc){
            teacher.value = ret2.res.teacher
        }
    }

    if(poss.value){
        const ret = await client.value.callApi('Get/GetCourseStus',{
            course:courseId
        })
        if(ret.isSucc){
            stus.value = ret.res.stus.map(s => s.student[0])
        }
    }

    loadingInstance.close()
})
</script>
<style lang="less" scoped>
.container {
    background-color: #f2f2f2;
    padding: 80px 0;
    padding-bottom: 10px;
    height: 100%;
    width: 100%;
    overflow: auto;

    .title {
        height: 60px;
        width: 100%;
        background-color: white;
        position: fixed;
        top: 0;
        z-index: 10;
        line-height: 60px;
        font-size: 20px;
        font-weight: bold;
        padding: 0 30px;
    }

    .head {
        height: auto;
        width: 100%;
        // display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        .portrait {
            height: 160px;
            // width: 120px;
            aspect-ratio: 16 / 9;
            border-radius: 10px;
            border: 5px solid white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }
        
        .name {
            height: 80px;
            /* width: 120px; */
            line-height: 80px;
            text-align: center;
            padding: 0 20px;
            font-size: 24px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
            color: #666666;
            // margin-left: 10px;
        }

        .blocks{
            height: 100px;
        }

        .block {
            height: 100px;
            width: 100px;
            background-color: white;
            margin: 0 12px;
            border-radius: 20px;
            box-shadow: 1px 1px 1px #0000000a;
            position: relative;
            padding: 0 20px;



            .count {
                height: 70px;
                font-size: 40px;
                padding-top: 25px;
                line-height: 40px;
                font-weight: bold;
            }

            .param {
                height: 40px;
                line-height: 30px;
                font-size: 13px;
                color: #a2a2a2;
            }
        }
    }

    .line {
        position: relative;
        width: 100%;
        background-color: white;
        padding: 0px 20px;
        padding-top: 30px;
        // height: 500px;
        // display: flex;
        margin-bottom: 30px;

        .intro {
            position: relative;
            margin: 0 auto;
            display: flex;
            height: 120px;
            justify-content: center;
            align-items: center;

        }

        .field {
            position: relative;
            padding: 0.5rem 1rem 0;
            margin: 40px auto 60px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
            background: rgba(255, 255, 255, .95);
            width: 70%;

            .legend {
                span {
                    background: #0fb2fc;
                    display: inline-block;
                    color: #fff;
                    padding: 0.2rem 0.5rem;
                    white-space: nowrap;
                    line-height: 1.5;
                    font-size: 13px;
                    margin-right: 10px;
                }
            }

            .item {
                line-height: 20px;
                padding: 14px;
                font-size: 14px;
                display: flex;
                color: #adadad;
                &>div{
                    // line-height: 20px;
                }

                &:hover {
                    background: rgba(0, 0, 0, .03);
                }

                &:nth-of-type(2n) {
                    background: rgba(0, 0, 0, .01);
                }

                &:last-child {
                    margin-bottom: 20px;
                }

                .attr {
                    flex: 0 0 100px;
                    font-weight: bold;
                }

                .content {
                    flex-grow: 1;
                    
                }
                .int{
                    line-height: 26px;
                }
            }
        }
        .stu-list{
            height: 60vh;
            overflow: auto;
        }

        .header {
            position: absolute;
            top: 20px;
            font-size: 16px;
            color: #a2a2a2;
        }

        .line-chart {
            width: 55%;
        }

        .bar-chart {
            width: 45%;
        }
    }
}

.ver {
    border-bottom: 0.0625rem solid rgb(226, 232, 240);
    border-top: 0.0625rem solid rgb(226, 232, 240);
}
</style>